// index.js
const util = require('../../utils/util.js');
const app = getApp();
const cloudUrl = app.globalData.cloudUrl;

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    app:app,
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    cloudUrl:app.globalData.cloudUrl,
    //幻灯片
    //background: ['demo-text-1', 'demo-text-2', 'demo-text-3', 'demo-text-4'],
    background: [{"id":1, "name":"大木山景区"}, {"id":2, "name":"白马湖"}, {"id":3, "name":"驼峰景区"}, {"id":4, "name":"湘湖"}],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    swiperUrls:[cloudUrl+"swiper1.jpg",cloudUrl+"pic/ms/1.jpg",cloudUrl+"pic/lch/1.jpg",cloudUrl+"pic/lq/1.png"],
    funBtnArray: [
      { id: 1, posx:0, posy:0, name: '景点简介',icon:"menu/jd.png" },
      { id: 2, posx:1, posy:0, name: '自助购票',icon:"menu/gp.png" },
      //{ id: 3, posx:2, posy:0, name: '景区导览',icon:"menu/jq.png" },
      { id: 3, posx:2, posy:0, name: '龙游风味',icon:"menu/cy.png" },
      { id: 4, posx:3, posy:0, name: '酒店预定',icon:"menu/ms.png" },
      { id: 5, posx:0, posy:1, name: '文创商城',icon:"menu/gwc.png" },
      //{ id: 6, posx:1, posy:1, name: '餐饮预定',icon:"menu/cy.png" },
      { id: 7, posx:1, posy:1, name: '评价建议',icon:"menu/pj.png" },
      //{ id: 8, posx:3, posy:1, name: '游玩攻略',icon:"menu/yw.png" },
    ],
    mustNames:[{name:"大木山茶园景区",fontSize:42},{name:"黄家大院景区",fontSize:32},{name:"松阴溪景区",fontSize:32},{name:"延庆寺塔景区",fontSize:32}],
    mustVisitUrls:[cloudUrl+"guideImg1.png"],
    titleTagUrl:cloudUrl+"titleTag.png",
    //热销门票
    hotPreMP:[{id:1, name:"黄家大院景区门票", price:30, oldPrice:30, time:"18:00"},{id:1, name:"大木山茶园景区观光车游览套票", price:30, oldPrice:30, time:"17:00"},{id:1, name:"松阴溪景区观光车往返游览票套票(只限在独山驿站...", price:50, oldPrice:50, time:"16:30"}],
    //热销商品
    hotPreShop:[{id:1, name:"松阳味道红糖", price:30},{id:1, name:"松阳味道黑香肠", price:30},{id:1, name:"大木山便携式旅行茶具套装",price:168},{id:1, name:"松阳龙井茶", price:30},{id:1, name:"农家天然笋干", price:30}],
    hotShopUrl:cloudUrl+"shopItem1.png",
    //热门餐厅
    hotRestt:[{id:1, name:"松阳故事", price:30,img:cloudUrl+"guideImg1.png"},{id:1, name:"青龙堰驿站", price:30,img:cloudUrl+"guideImg1.png"}],
    testShow:false
  },
  onLoad(e){
    //wx.cloud.init({env:'buxucloud-01-7gvvu046d141963a'});//初始化云开发
    //this.sendInfoToServer();
    //const app = getApp();
    //console.log("----index--------")
    //console.log(app.globalData.scenicInfos);
    //获取景区列表
    const _this = this;//
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data:{
        type:"getDataList",
        todos:"scenicList"
      },
      complete: res => {
        const infos = res.result.data;
        var arr = [];
        var info = null;
        var index = 0;
        for(var i=0; i<infos.length; i++){
          info = infos[i];
          if(info.isHot == true){
            arr.push(info);
            _this.data.background[index+1] = info;
            _this.data.swiperUrls[index+1] = _this.data.cloudUrl + "pic/" + info.swipers[0];
            index ++;
          }
        }
        _this.setData({mustNames:arr,background:_this.data.background,swiperUrls:_this.data.swiperUrls});
      }
    });
    //获取景区列表
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data:{
        type:"getDataList",
        todos:"ticketsList"
      },
      complete: res => {
        console.log("get ticketsList,",res);
        const infos = res.result.data;
        var arr = [];
        for(var i=0; i<infos.length; i++){
          if(infos[i].isHot == true){
            arr.push(infos[i]);
          }
        }
        _this.setData({hotPreMP:arr});
      }
    })
    //
  },
  onResttClick(e){
    util.gotoResttPage(e);
  },
  onShopDetailsClick(e){
    util.gotoShopDetailsPage(e);//
  },
  //发送用户信息到服务端
  sendInfoToServer(){
    // 在小程序代码中：
    wx.cloud.callFunction({
      name: 'add',
      complete: res => {
        console.log('callFunction test result: ', res);
        wx.setNavigationBarTitle({
          title: "test="+res.result.openid,
        })
      }
    })
  },
  onTicketsBuyClick(e){
    const idx = e.currentTarget.dataset.index;
    var info = this.data.hotPreMP[idx];
    info = JSON.stringify(info);
    util.gotoTicketsBuyPage(info);
  },
  gotoTicketsList(){
    util.gotoTicketsList();
  },
  onSwiperClick(e){
    const item = e.currentTarget.dataset.item;
    if(item.id == 1){
      this.gotoMyInfo(e);
    }
    else{
      util.gotoScenic(item);
    }
  },
  onScenicClick(e){
    util.gotoScenic(this.data.mustNames[e.currentTarget.dataset.ni])
  },
  gotoServiceTalk(e){
    //var app = getApp()
    console.log(app.globalData)
    util.gotoServiceTalk()
  },
  gotoMyInfo(e){
    //var app = getApp()
    util.gotoMyInfo()
  },
  onFunBtnClick(e) {
    console.log(e);
    console.log(this);
    const item = e.currentTarget.dataset.item;
    switch(item.id){
      case 1:
        util.gotoScenicList();
        //this.sendInfoToServer();
        //this.getUserInfo();
      break;
      case 2:
        util.gotoTicketsList();
      break;
      case 3:
        //this.onOpenMap(e);
        var tabs = [1,0];
        util.gotoShopListPage(tabs);
      break;
      case 4:
        util.gotoHotelsListPage();
      break;
      case 5:
        var tabs = [1,1];
        util.gotoShopListPage(tabs);
      break;
      case 6:
        this.gotoResttListPage(1);
      break;
      case 7:
        util.gotoAppreisePage();
      break;
      case 8:
        util.gotoPlayGuidePage();
      break;
    }
  },
  gotoShopListPage(e){
    util.gotoShopListPage(e);
  },
  gotoResttListPage(e){
    util.gotoResttListPage(e);
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    console.log('sdsdkjfssdkl')
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onOpenMap(e) {
    wx.navigateTo({
      url: '../map/map'
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
